package netbayServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import netbay.UserManager;

/**
 * Servlet implementation class Logout
 */
public class Logout extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Logout() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		UserManager um = new UserManager();
		 try {
			um.saveLog((String)session.getAttribute("userId"),UserManager.actionLogout,"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		session.setAttribute("userId",null);
    	session.setAttribute("group",null);
    	session.setAttribute("tcdTracking",null);
    	session.setAttribute("rdTracking",null);
    	session.setAttribute("tcdBilling",null);
    	session.setAttribute("rdBilling",null);
    	session.setAttribute("manageUsers",null);
    	session.setAttribute("manageGroups",null);
    	session.setAttribute("userLog",null);
    	session.setAttribute("reTCDTCD",null);
    	session.setAttribute("reJPMCTCD",null);
    	session.setAttribute("reRDRD",null);
    	session.setAttribute("reJPMCRD",null);         
    	session.invalidate();
         String  url = "/Login.jsp";
         request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		 session.setAttribute("session", null);
         session.invalidate();
         String  url = "/Login.jsp";
         request.getRequestDispatcher(url).forward(request, response);
	}

}
